/*
Date:20210530 21:17PM
key:三种文化衫，下属不能跟领导撞衫，每名员工穿对应的文化衫都有个快乐值，要求总快乐最大。
    只过了35%,只考虑领导最快了的情况而不是总的最快乐
*/
#include <iostream>
#include <vector>
#include<unordered_set>
#include<set>
using namespace std;
int sel(vector<int>&v, int& a)
{
	int xiabiao;
	int Max = 0;
	int i;
	for (i=0;i<3;i++)
	{
		if (Max < v[i]) { Max = v[i]; xiabiao = i; }
	}
	return xiabiao;
}
int maxe(vector<int>& v)
{
	int re = 0;
	for (int i = 0; i < v.size(); i++)
	{
		re = max(re, v[i]);
	}
	return re;
}
int main()
{
	int n;
	cin >> n;
	vector<vector<int>>v1;
	v1.resize(n);
	for (int i = 0; i < n; i++)
	{
		vector<int>v;
		v.resize(3);
		for (int j = 0; j < 3; j++)
		{
			cin >> v[j];
		}
		v1[i] = v;

	}
	int a, b;
	for (int i = 0; i < n-1; i++)
	{
		cin >> a >> b;
		//挑出a最大下标然后在b里屏蔽这个
		v1[b][sel(v1[a], a)]=0;
	}
	int re = 0;
	for (int i = 0; i < n; i++)
	{
		
		re += maxe(v1[i]);
	}
	cout << re;
}